package com.dindang.propertyfee.mvp.ui.adapter.custom;

import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dindang.propertyfee.R;
import com.dindang.propertyfee.mvp.model.entity.PropertyCharge;
import com.longxi.gamer_base.utils.TextViewUtils;

import java.util.List;

/**物业费逾期适配器*/
public class OwnerSearchViewAdapter extends RecyclerView.Adapter<OwnerSearchViewAdapter.ViewHolder> {
    List<PropertyCharge.RecentBill.Rows> list;
    OnOwnerSearchListener listener;
    String keyword;
    public OwnerSearchViewAdapter(List<PropertyCharge.RecentBill.Rows> list, OnOwnerSearchListener listener) {
        this.list = list;
        this.listener = listener;
        this.keyword = "";
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.propertyfee_item_owner_search, parent, false);
        OwnerSearchViewAdapter.ViewHolder viewHolder = new OwnerSearchViewAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PropertyCharge.RecentBill.Rows rows = list.get(position);

        if(rows.addr.contains(keyword)){
            holder.tv_address.setText(TextViewUtils.getKeywordContainsShowColor(holder.tv_address.getContext(), rows.addr, keyword));
        }else{
            holder.tv_address.setText(rows.addr);
        }

        if(rows.custName.contains(keyword)){
            holder.tv_name.setText(TextViewUtils.getKeywordContainsShowColor(holder.tv_name.getContext(), rows.custName, keyword));
        }else{
            holder.tv_name.setText(rows.custName);
        }

        holder.constraintlayout.setOnClickListener(v -> listener.onClick(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        ConstraintLayout constraintlayout;
        TextView tv_address;
        TextView tv_name;
        ViewHolder(View itemView) {
            super(itemView);
            constraintlayout = itemView.findViewById(R.id.propertyfee_constraintlayout);
            tv_address = itemView.findViewById(R.id.propertyfee_tv_address);
            tv_name = itemView.findViewById(R.id.propertyfee_tv_name);
        }
    }

    public interface OnOwnerSearchListener{
        void onClick(int position);
    }

}